package com.li.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ndboo.reslibrary.R;

/**
 * Created by Li on 2017/3/2.
 * multipleLayout
 */

public class MultipleLayout extends FrameLayout implements View.OnClickListener {

    public static final int STATUS_LOADING = 1;
    public static final int STATUS_SUCCESS = 2;
    public static final int STATUS_FAILURE = 3;
    public static final int STATUS_EMPTY = 4;
    private LayoutParams mLayoutParams;
    private View viewLoading;
    private View viewContent;
    private View viewFailure;
    private View viewEmpty;
    private OnMultipleClickListener mListener;
    private TextView mTxtLoading;
    private RotateAnimation mAnimation;

    public MultipleLayout(Context context) {
        super(context);
    }
    public MultipleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (mLayoutParams == null) {
            mLayoutParams = new LayoutParams(-1, -1);
            mLayoutParams.gravity = Gravity.CENTER;
        }
        initAttr(context, attrs);
    }

    public View getViewLoading() {
        return viewLoading;
    }

    public View getViewFailure() {
        return viewFailure;
    }

    public View getViewEmpty() {
        return viewEmpty;
    }

    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MultipleLayout);
        int loadingLayoutId = array.getResourceId(R.styleable.MultipleLayout_layout_loading, -1);
        int failLayoutId = array.getResourceId(R.styleable.MultipleLayout_layout_failure, -1);
        int emptyLayoutId = array.getResourceId(R.styleable.MultipleLayout_layout_empty, -1);


        viewLoading = LayoutInflater.from(context)
                .inflate(loadingLayoutId == -1 ? R.layout.layout_loading : loadingLayoutId, null);

        ImageView imgLoading = viewLoading.findViewById(R.id.img_loading);
        mTxtLoading = viewLoading.findViewById(R.id.txt_loading);

        mAnimation = (RotateAnimation) AnimationUtils.loadAnimation(context, R.anim.rotate_pro);
        mAnimation.setDuration(3000);
        mAnimation.setInterpolator(new LinearInterpolator());
        mAnimation.setRepeatCount(Animation.INFINITE);
        imgLoading.setAnimation(mAnimation);


        viewFailure = LayoutInflater.from(context)
                .inflate(failLayoutId == -1 ? R.layout.layout_failure : failLayoutId, null);
        viewEmpty = LayoutInflater.from(context)
                .inflate(emptyLayoutId == -1 ? R.layout.layout_empty : emptyLayoutId, null);

        LayoutParams loadingParams=new LayoutParams(-2,-2);
        loadingParams.gravity=Gravity.CENTER;
        addView(viewLoading, loadingParams);
        addView(viewFailure, mLayoutParams);
        addView(viewEmpty, mLayoutParams);
        array.recycle();

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int count = getChildCount();

        if (count > 0) {
            viewContent = getChildAt(count - 1);
            viewEmpty.setTag("empty");
            viewFailure.setTag("failure");
            viewEmpty.setOnClickListener(this);
            viewFailure.setOnClickListener(this);
//            onLoading();
            onSuccess();
        }

        viewLoading.bringToFront();
    }

    private void switchLayout(int layoutStatus) {
        viewLoading.setVisibility(layoutStatus == STATUS_LOADING ? VISIBLE : GONE);
        viewContent.setVisibility(layoutStatus == STATUS_SUCCESS ? VISIBLE : GONE);
        viewFailure.setVisibility(layoutStatus == STATUS_FAILURE ? VISIBLE : GONE);
        viewEmpty.setVisibility(layoutStatus == STATUS_EMPTY ? VISIBLE : GONE);
    }

    /**
     * 显示加载中界面
     */
    public void onLoading() {
        mTxtLoading.setVisibility(VISIBLE);
        mAnimation.start();
        switchLayout(STATUS_LOADING);
        mTxtLoading.setText("加载中");
    }

    /**
     * 加载界面带文字
     * @param loadingTxt    加载文字
     */
    public void onLoading(String loadingTxt){
        mTxtLoading.setVisibility(VISIBLE);
        mAnimation.start();
        switchLayout(STATUS_LOADING);
        mTxtLoading.setText(loadingTxt);
    }

    /**
     * 显示成功界面
     */
    public void onSuccess() {
        mAnimation.cancel();
        mTxtLoading.setVisibility(GONE);
        switchLayout(STATUS_SUCCESS);
    }


    /**
     * 显示失败界面
     */
    public void onFailure() {
        mAnimation.cancel();
        switchLayout(STATUS_FAILURE);
    }

    /**
     * 显示返回为空界面
     */
    public void onEmpty() {
        mAnimation.cancel();
        switchLayout(STATUS_EMPTY);
    }

    /**
     * 进度条和内容一起显示
     */
    public void onLoadingWithContent() {

        viewLoading.setVisibility(VISIBLE);
        viewContent.setVisibility(VISIBLE);
        viewFailure.setVisibility(GONE);
        viewEmpty.setVisibility(GONE);

        mAnimation.start();
        mTxtLoading.setVisibility(VISIBLE);
        mTxtLoading.setText("加载中");
    }
    /**
     * 带加载文字
     * @param loadingTxt    加载文字
     */
    public void onLoadingWithContent(String loadingTxt) {
        viewLoading.setVisibility(VISIBLE);
        viewContent.setVisibility(VISIBLE);
        viewFailure.setVisibility(GONE);
        viewEmpty.setVisibility(GONE);

        mAnimation.start();
        mTxtLoading.setVisibility(VISIBLE);
        mTxtLoading.setText(loadingTxt);
    }




    public void setOnMultipleClickListener(OnMultipleClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            switch (v.getTag().toString()) {
                case "empty":
                    mListener.emptyClickListener();
                    break;
                case "failure":
                    mListener.failureClickListener();
                    break;
            }
        }
    }

    public interface OnMultipleClickListener {
        /**
         * 数据为空时点击操作
         */
        void emptyClickListener();

        /**
         * 出现错误时点击操作
         */
        void failureClickListener();
    }
}